package com.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bean.Asn;
import com.bean.AsnCancel;
import com.bean.AsnDetail;
import com.bean.ReturnMsg;
import com.bean.wms.Receipt;
import com.bean.wms.ReceiptDetail;
import com.dao.AsnDao;
import com.dao.wmsdao.ReceiptDao;
import com.dao.wmsdao.VerifyDao;
import com.domain.beanutil.BeanConvertUtil;
import com.domain.beanutil.DataConvertUtil;
import com.domain.beanutil.DataUtilInterface;
import com.service.ReceiptService;
import com.service.VerifyService;

/** 
* @ClassName: ReceiptServiceImpl 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author wangxm
* @date 2014-6-16 下午4:16:14 
*/
@Service("receiptService")
@Transactional(rollbackFor=Exception.class)
public class ReceiptServiceImpl implements ReceiptService {
	
	@Autowired 
	private ReceiptDao receiptDao;
	
	@Autowired
	private AsnDao asnDao;
	
	@Autowired
	private VerifyDao verifyDao;
	
	@Autowired
	private VerifyService verifyService;
	
	
	@Override
	public ReturnMsg getAsn(Asn asn) {
	ReturnMsg returnMsg = new ReturnMsg(asn.getStorer(), asn.getWmwhseid(), asn.getExternalno(), "", "", "");
//		
//		try {
//			Receipt receipt = BeanConvertUtil.AsnConvertReceipt(asn);     
//			if(receipt.getWhseid() == null ||"".equals(receipt.getWhseid())){
//				receipt.setWhseid(DataUtilInterface.defaultwhseid);
//			}
//			String errMsg = verifyService.verifyReceipt(receipt);
//			if("".equals(errMsg)){
//				Receipt receipt2 = receiptDao.getReceiptByKey(receipt.getWhseid(), receipt.getExternreceiptkey(), receipt.getStorerkey());
//				if(receipt2 == null){
//		    		String receiptKey = DataConvertUtil.headLiuShui((verifyDao.getKeycount(receipt.getWhseid(), DataUtilInterface.receiptKeyName)+1)+"");
//		    		receipt.setReceiptkey(receiptKey);
//					receiptDao.insertReceipt(receipt);
//					for (ReceiptDetail receiptDetail : receipt.getItemList()) {
//						receiptDetail.setReceiptkey(receipt.getReceiptkey());
//						receiptDetail.setPackkey(verifyDao.getPackKeyFromSku(receiptDetail.getWhseid(), receiptDetail.getSku(), receiptDetail.getStorerkey()));
//						receiptDao.insertReceiptDetail(receiptDetail);
//					}
//					
//					asnDao.insertAsn(asn);
//					for (AsnDetail asnDetail : asn.getItemList()) {
//						asnDao.insertAsnDetail(asnDetail);
//					}
//					
//					returnMsg.setSuccess(DataUtilInterface.SUC_RUSLT_Y);
//					returnMsg.setNotes(DataUtilInterface.NOTES_SUC);
//				}else{
//					returnMsg.setSuccess(DataUtilInterface.SUC_RUSLT_R);
//		    		returnMsg.setErrmsg(DataUtilInterface.ERROR_MSG_HAVING_ORTHER+receipt.getExternreceiptkey());
//				}
//			}else{
//				returnMsg.setSuccess(DataUtilInterface.SUC_RUSLT_N);
//	    		returnMsg.setErrmsg(errMsg);
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//			returnMsg.setSuccess(DataUtilInterface.SUC_RUSLT_N);
//    		returnMsg.setErrmsg(DataUtilInterface.ERROR_MSG_INSERT_ERROR+asn.getExternalno()+","+e.getMessage());
//		}
//		
		return returnMsg;
	}

	@Override
	public ReturnMsg getAsnCancel(AsnCancel asnCancel) {
		Receipt receipt = receiptDao.getReceiptByKey(asnCancel.getWmwhseid(), asnCancel.getExternalno(), asnCancel.getStorer());
		ReturnMsg returnMsg = new ReturnMsg(asnCancel.getStorer(), asnCancel.getWmwhseid(), asnCancel.getExternalno(), "", "", "");
	    if(receipt == null){
	    	returnMsg.setSuccess(DataUtilInterface.SUC_RUSLT_N);
	    	returnMsg.setErrmsg(DataUtilInterface.ERROR_MSG_NO_DATA);
	    	return returnMsg;
	    }
	    if(!"0".equals(receipt.getStatus())){
	    	returnMsg.setSuccess(DataUtilInterface.SUC_RUSLT_N);
	    	returnMsg.setErrmsg(DataUtilInterface.CANCEL_ERROR_MSG_ASN_CANNOT_CANCEL);
	    	return returnMsg;
	    }
	    
		if(receipt.getWhseid() == null ||"".equals(receipt.getWhseid())){
			receipt.setWhseid(DataUtilInterface.defaultwhseid);
		}
		
		receipt.setStatus("20");
	    receipt.setWarehousereference(asnCancel.getReason());
	    receipt.setNotes(asnCancel.getReason());
	    receipt.setType(asnCancel.getReceiptype());

	    try {
			receiptDao.updateReceipt(receipt);
		    returnMsg.setSuccess(DataUtilInterface.SUC_RUSLT_Y);
	        asnDao.insertAsnCancel(asnCancel);
		} catch (Exception e) {
			e.printStackTrace();
			returnMsg.setSuccess(DataUtilInterface.SUC_RUSLT_N);
			returnMsg.setErrmsg(DataUtilInterface.CANCEL_ERROR_MSG_ERROR+","+e.getMessage());
			return returnMsg;
		}

		return returnMsg;
	}
}
